package org.bees.cms.web.util;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;

import org.apache.velocity.app.Velocity;
import org.bees.cms.common.Files;
import org.bees.cms.web.util.velocity.Context;

public abstract class Staticizer {

	public static void staticize(String path, Context context, String template) throws Exception {
		OutputStream os = null;
		OutputStreamWriter osw = null;
		Writer writer = null;
		try {
			File file = new File(Files.absolute(path));
			if (!file.getParentFile().exists()) {
				file.mkdirs();
			}
			os = new FileOutputStream(file);
			osw = new OutputStreamWriter(os);
			writer = new BufferedWriter(osw);
			Velocity.evaluate(context, writer, "", template);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (writer != null) {
				writer.close();
			}
			if (osw != null) {
				osw.close();
			}
			if (os != null) {
				os.close();
			}
		}
	}

}
